package cc.ipost.ipost;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;
import cc.ipost.ipost.data.RequestData;

public class WebService {
	RequestData rd;
	HttpClient hc;
	HttpResponse res;
	int connTimeout = 30000;
	int sockTimeout = 30000;

	static String TAG = "WebService";

	public WebService(RequestData rd) {
		this.rd = rd;
		hc = new DefaultHttpClient();
		HttpParams params = hc.getParams();
		HttpConnectionParams.setConnectionTimeout(params, connTimeout);
		HttpConnectionParams.setSoTimeout(params, sockTimeout);
	}

	public HttpResponse request() {
		try {
			switch (rd.method) {
			case RequestData.GET: {
				return get();
			}
			case RequestData.POST: {
				return post();
			}
			case RequestData.PUT: {
				return put();
			}
			case RequestData.DELETE: {
				return delete();
			}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	private HttpResponse get() throws ClientProtocolException, IOException {
		Log.i(TAG, rd.url);
		HttpGet req = new HttpGet(rd.url);

		res = hc.execute(req);
		return handleResult();

	}

	private HttpResponse post() throws ClientProtocolException, IOException {
		HttpPost req = new HttpPost(rd.url);
		// hp.setHeader("Content-Type", "application/x-www-form-urlencoded");
		MultipartEntity entity = configParams();
		req.setEntity(entity);
		res = new DefaultHttpClient().execute(req);
		return handleResult();

	}

	private HttpResponse put() throws ClientProtocolException, IOException {
		HttpPut req = new HttpPut(rd.url);
		MultipartEntity entity = configParams();
		req.setEntity(entity);
		res = hc.execute(req);
		return handleResult();
	}

	private HttpResponse delete() throws ClientProtocolException, IOException {
		HttpDelete req = new HttpDelete(rd.url);
		res = hc.execute(req);
		return handleResult();

	}

	private MultipartEntity configParams() throws UnsupportedEncodingException {
		MultipartEntity entity = new MultipartEntity();

		for (String key : rd.params.keySet()) {
//			String v = java.net.URLEncoder.encode(rd.params.get(key).toString());
			String v = rd.params.get(key).toString();
			Log.i("posrt args", key + " : " + v);
			entity.addPart(key, new StringBody(v, Charset.forName("UTF-8")));
		}
		if (rd.fileField != null && rd.filepath != null) {
			File file = new File(rd.filepath);
			entity.addPart(rd.fileField, new FileBody(file));
		}
		return entity;
	}
	
	/*
	private String handleResult() throws ParseException, IOException {
		int code = res.getStatusLine().getStatusCode();
		Log.i(this.TAG, "code: " + code);
		if (code > 199 && code < 399) {
			String en = EntityUtils.toString(res.getEntity());
			Log.i(this.TAG, "entity: " + en);
			return en;
		}
		return null;
	}
	*/
	private HttpResponse handleResult() {
		return res;
	}

}
